package it.leehook.mp3.activities;

import it.leehook.mp3.R;
import it.leehook.mp3.utils.Constants;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.DisplayMetrics;
import android.widget.Toast;

/**
 * Preferenze dell'app
 * 
 * @author l.angelini
 * 
 */
public class Preferences extends PreferenceActivity {

	private static final int PICK_DIR_MUSIC = 0;
	private static final int PICK_DIR_WORK = 1;

	private static final int ES_GESTORE_FILE = 1;
	private static final int OI_FILE_MANAGER = 2;

	private Preference buttonMusic;

	private EditTextPreference editTxtMusic;

	private Preference buttonWork;

	private EditTextPreference editTxtWork;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.preferences);
		editTxtMusic = (EditTextPreference) findPreference(Constants.MUSIC_PREFS);
		getPreferenceScreen().removePreference(editTxtMusic);
		buttonMusic = (Preference) findPreference(Constants.MUSIC_BTN_PREFS);
		buttonMusic
				.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						pickDirectory(PICK_DIR_MUSIC);
						return true;
					}
				});
		editTxtWork = (EditTextPreference) findPreference(Constants.WORK_PREFS);
		getPreferenceScreen().removePreference(editTxtWork);
		buttonWork = (Preference) findPreference(Constants.WORK_BTN_PREFS);
		buttonWork
				.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						pickDirectory(PICK_DIR_WORK);
						return true;
					}
				});
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		Resources res = getApplicationContext().getResources();
		DisplayMetrics dm = res.getDisplayMetrics();
		android.content.res.Configuration conf = res.getConfiguration();
		res.updateConfiguration(conf, dm);
	}

	/**
	 * This is called after the ES File Manager finished.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode != RESULT_OK || data == null) {
			return;
		}

		Uri uri = data.getData();

		switch (requestCode) {
		case PICK_DIR_MUSIC:
			buttonMusic.setSummary(uri.getPath());
			editTxtMusic.setText(uri.getPath());
			break;
		case PICK_DIR_WORK:
			buttonWork.setSummary(uri.getPath());
			editTxtWork.setText(uri.getPath());
			break;
		}
	}

	/**
	 * Recupera la directory tentando nell'ordine di utilizzare: <br>
	 * 1. ES Gestore File <br>
	 * 2. OI File Manager
	 */
	private void pickDirectory(int type) {

		Intent intentES = getIntent("com.estrongs.action.PICK_DIRECTORY",
				getString(R.string.pick_title), ES_GESTORE_FILE);
		intentES.setData(Uri.parse("file://" + "/sdcard"));

		Intent intentOI = getIntent("org.openintents.action.PICK_DIRECTORY",
				getString(R.string.pick_title), OI_FILE_MANAGER);
		intentOI.setData(Uri.parse("file://" + "/sdcard"));
		try {
			startActivityForResult(intentES, type);
		} catch (ActivityNotFoundException e1) {
			try {
				startActivityForResult(intentOI, type);
			} catch (ActivityNotFoundException e2) {
				try {
					Intent importTree = new Intent(Intent.ACTION_GET_CONTENT);
					importTree.setData(Uri.parse("file://" + "/sdcard"));
					Intent fileMan = Intent.createChooser(importTree,
							getString(R.string.pick_title));
					startActivityForResult(fileMan, type);
				} catch (ActivityNotFoundException e3) {
					Toast.makeText(this,
							getString(R.string.no_filemanager_installed),
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	/**
	 * Valorizza la intent
	 * 
	 * @param action
	 * @param btnTitle
	 * @param fileManager
	 * @return
	 */
	private Intent getIntent(String action, String btnTitle, int fileManager) {
		Intent intent = new Intent(action);
		if (btnTitle != null) {
			switch (fileManager) {
			case ES_GESTORE_FILE:
				intent.putExtra("com.estrongs.intent.extra.BUTTON_TITLE",
						btnTitle);
				break;
			case OI_FILE_MANAGER:
				intent.putExtra("org.openintents.extra.TITLE", btnTitle);
				break;
			default:
				break;
			}
		}
		return intent;
	}
}
